package com.itheima.netty.c8;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;

public class MyHttpServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
        String methodName = msg.method().name();
        String requestUri = msg.uri();
        FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.copiedBuffer("Hello World".getBytes()));
        response.headers().add(HttpHeaderNames.CONTENT_TYPE, "text/plain");
        System.out.println(String.format("methodName:%s,requestUri:%s", methodName, requestUri));
        ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    }
}
